#ifndef MCLIB_PROCESS_ERROR_HPP
#define MCLIB_PROCESS_ERROR_HPP

#include "config.hpp"

#include <string>
#include <exception>
#include <stdexcept>

NAMESPACE_STD
NAMESPACE_PROCESS

class SystemError : public std::runtime_error
{
public:
#if defined(CPP_POSIX_API)
    typedef int ErrorCode;
#elif defined(CPP_WINDOWS_API)
    typedef DWORD ErrorCode;
#endif

    SystemError(const ErrorCode _err_code, const std::string & what_arg)
		: std::runtime_error(what_arg), error_code_(_err_code) {}

    SystemError(const ErrorCode _err_code, const char* what_arg)
	: std::runtime_error(what_arg), error_code_(_err_code) {}

	SystemError(const ErrorCode _err_code)
	: std::runtime_error(""), error_code_(_err_code) {}

	virtual ~SystemError() throw() {}

	const ErrorCode  error_code() const throw() { return error_code_; }
	const char *     what()       const throw();

private:
	ErrorCode         error_code_;
	mutable std::string  what_;
};

//  implementation  ------------------------------------------------------//

inline const char * SystemError::what() const throw()
{
	if ( what_.empty() )
	{
		try
		{
			what_ = this->std::runtime_error::what();
			if ( !what_.empty() ) what_ += ": ";
			//m_what += m_error_code.message();
		}
		catch (...) { return std::runtime_error::what(); }
	}
	return what_.c_str();
}

END_NAMESPACE
END_NAMESPACE

#endif
